package com.example.myapplication.z_a_home

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.example.myapplication.R
import com.example.myapplication.z_b_details.DetailsFragment
import com.example.youhuo.a_fragment.*
import com.example.youhuo.z_a_onepage.Splash_Custom_View
import com.example.youhuo.z_a_onepage.Splash_dialog
import com.example.youhuo.z_a_onepage.fragment.One_Fragment

class HomeActivity : AppCompatActivity(), Splash_Custom_View.ClickBarlistener, Fragment_Conteact {

    private val oneTopView: View by lazy { findViewById<View>(R.id.one_top_view) }
    private val oneView: Splash_Custom_View by lazy { findViewById<Splash_Custom_View>(R.id.one_view) }
    private var fragment: Fragment? = null
    private lateinit var fragmentmap: MutableMap<Fragment_Conteact.FragmentType, Fragment>



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //注册广播
//        var intfi = IntentFilter()
//        intfi.addAction("sendname")
//        registerReceiver(broad,intfi)
        //展示dialog
        var splash = Splash_dialog(this)
        splash.create_Dialog()

        initView()

    }


    private fun initView() {


        oneView.addImage(R.mipmap.tabbar_homepage_normal)
        oneView.addImage(R.mipmap.tabbar_category_normal)
        oneView.addImage(R.mipmap.tabbar_cart_normal)
        oneView.addImage(R.mipmap.tabbar_see_normal)
        oneView.addImage(R.mipmap.tabbar_mine_normal)
        oneView.listener(this)
        oneView.show()

        //第一次展示第一个页面
        showFragment(Fragment_Conteact.FragmentType.One)
    }

    override fun OnClickItem(postion: Int) {

        when (postion) {
            0 -> showFragment(Fragment_Conteact.FragmentType.One)
            1 -> showFragment(Fragment_Conteact.FragmentType.Two)
            2 -> showFragment(Fragment_Conteact.FragmentType.Three)
            3 -> showFragment(Fragment_Conteact.FragmentType.Four)
            4->  showFragment(Fragment_Conteact.FragmentType.FIVE)
//                var boolean = getSharedPreferences("islog", Context.MODE_PRIVATE).getBoolean("log", false)
//                if (boolean) {
//                    showFragment(Fragment_Conteact.FragmentType.FIVE)
//                } else {
//                    startActivity(Intent(this, LoginActivity::class.java))
//
//                }



        }
    }

    override fun showFragment(type: Fragment_Conteact.FragmentType) {
        fragmentmap = mutableMapOf()

        if (fragment == null) {
            addFragment(findfragment(type)!!, R.id.one_linear)
        }else{
            if (fragmentmap.containsKey(type)){
                hideFragment(fragment!!)
                showFragment(fragmentmap.get(type)!!)
            }else{
                hideFragment(fragment!!)
                addFragment(findfragment(type)!!, R.id.one_linear)
            }
        }
        fragment = findfragment(type)

    }

   fun findfragment(type: Fragment_Conteact.FragmentType): Fragment? {

        when (type) {
            Fragment_Conteact.FragmentType.One -> {
                if (!fragmentmap.containsKey(type)) {
                    fragmentmap.put(type, One_Fragment())
                }
                return fragmentmap.get(type)
            }

            Fragment_Conteact.FragmentType.Two -> {
                if (!fragmentmap.containsKey(type)) {
                    fragmentmap.put(type, DetailsFragment())
                }
                return fragmentmap.get(type)
            }


            Fragment_Conteact.FragmentType.Three ->{
                if (!fragmentmap.containsKey(type)) {
                    fragmentmap.put(type, Car_Fragment())
                }
                    return fragmentmap.get(type)

            }

            Fragment_Conteact.FragmentType.Four -> {
                if (!fragmentmap.containsKey(type)) {
                    fragmentmap.put(type, Community_Fragment())
                }
                return fragmentmap.get(type)
            }


            Fragment_Conteact.FragmentType.FIVE -> {
                if (!fragmentmap.containsKey(type)) {
                    fragmentmap.put(type, Mine_Fragment())
                }
                return fragmentmap.get(type)

            }

        }

    }


    fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int) {
        supportFragmentManager.inTransaction { add(frameId, fragment) }
    }

    fun AppCompatActivity.hideFragment(fragment: Fragment) {
        supportFragmentManager.inTransaction { hide( fragment) }
    }
    fun AppCompatActivity.showFragment(fragment: Fragment) {
        supportFragmentManager.inTransaction { show( fragment) }
    }


    //扩展函数
    inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
        val fragmentTransaction = beginTransaction()
        fragmentTransaction.func()
        fragmentTransaction.commit()
    }
}
